@echo off
setlocal enabledelayedexpansion

REM PPH Dependencies Installation Script (English Version)
REM Automatically install all necessary dependencies

echo ========================================
echo PPH Dependencies Installation
echo ========================================
echo.

set "DEPLOY_DIR=%~dp0.."
set "APP_DIR=%DEPLOY_DIR%\app"

echo [INFO] Starting dependencies installation...
echo [INFO] Deploy directory: %DEPLOY_DIR%
echo.

REM Check Node.js environment
node --version >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Node.js not found
    echo Please install Node.js 18.17.0 LTS first
    echo Download: https://nodejs.org/dist/v18.17.0/node-v18.17.0-x64.msi
    pause
    exit /b 1
)

for /f "tokens=*" %%i in ('node --version') do set NODE_VERSION=%%i
echo [OK] Node.js version: %NODE_VERSION%

REM Check npm
npm --version >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] npm not found
    pause
    exit /b 1
)

for /f "tokens=*" %%i in ('npm --version') do set NPM_VERSION=%%i
echo [OK] npm version: %NPM_VERSION%
echo.

REM Configure npm
echo [INFO] Configuring npm...
npm config set registry https://registry.npmjs.org/
npm config set fund false
npm config set audit false
echo [OK] npm configured
echo.

REM Install global dependencies
echo [STEP 1] Installing Global Dependencies
echo ========================================

echo [INFO] Installing PM2...
npm list -g pm2 >nul 2>&1
if %errorlevel% neq 0 (
    npm install -g pm2
    if %errorlevel% neq 0 (
        echo [ERROR] PM2 installation failed
        pause
        exit /b 1
    )
    echo [OK] PM2 installed
) else (
    echo [OK] PM2 already installed
)

echo [INFO] Installing pm2-windows-startup...
npm list -g pm2-windows-startup >nul 2>&1
if %errorlevel% neq 0 (
    npm install -g pm2-windows-startup
    if %errorlevel% neq 0 (
        echo [WARNING] pm2-windows-startup installation failed
        echo Auto-startup may not work properly
    ) else (
        echo [OK] pm2-windows-startup installed
    )
) else (
    echo [OK] pm2-windows-startup already installed
)
echo.

REM Install root dependencies
echo [STEP 2] Installing Root Dependencies
echo ========================================

cd /d "%DEPLOY_DIR%"
if exist "package.json" (
    echo [INFO] Installing root dependencies...
    npm install --production --no-optional
    if %errorlevel% neq 0 (
        echo [ERROR] Root dependencies installation failed
        pause
        exit /b 1
    )
    echo [OK] Root dependencies installed
) else (
    echo [INFO] No root package.json found, skipping
)
echo.

REM Install backend dependencies
echo [STEP 3] Installing Backend Dependencies
echo ========================================

if exist "%APP_DIR%\backend\package.json" (
    echo [INFO] Installing backend dependencies...
    cd /d "%APP_DIR%\backend"
    
    REM Clean existing node_modules if exists
    if exist "node_modules" (
        echo [INFO] Cleaning existing node_modules...
        rmdir /s /q node_modules
    )
    
    npm install --production --no-optional
    if %errorlevel% neq 0 (
        echo [ERROR] Backend dependencies installation failed
        echo Trying with --force flag...
        npm install --production --no-optional --force
        if %errorlevel% neq 0 (
            echo [ERROR] Backend dependencies installation failed completely
            pause
            exit /b 1
        )
    )
    echo [OK] Backend dependencies installed
) else (
    echo [WARNING] Backend package.json not found
)
echo.

REM Install frontend dependencies
echo [STEP 4] Installing Frontend Dependencies
echo ========================================

if exist "%APP_DIR%\frontend\package.json" (
    echo [INFO] Installing frontend dependencies...
    cd /d "%APP_DIR%\frontend"
    
    REM Clean existing node_modules if exists
    if exist "node_modules" (
        echo [INFO] Cleaning existing node_modules...
        rmdir /s /q node_modules
    )
    
    npm install --no-optional
    if %errorlevel% neq 0 (
        echo [ERROR] Frontend dependencies installation failed
        echo Trying with --force flag...
        npm install --no-optional --force
        if %errorlevel% neq 0 (
            echo [ERROR] Frontend dependencies installation failed completely
            pause
            exit /b 1
        )
    )
    echo [OK] Frontend dependencies installed
) else (
    echo [WARNING] Frontend package.json not found
)
echo.

REM Install management dependencies
echo [STEP 5] Installing Management Dependencies
echo ========================================

if exist "%DEPLOY_DIR%\management\package.json" (
    echo [INFO] Installing management dependencies...
    cd /d "%DEPLOY_DIR%\management"
    
    npm install --production --no-optional
    if %errorlevel% neq 0 (
        echo [WARNING] Management dependencies installation failed
        echo Management interface may not work properly
    ) else (
        echo [OK] Management dependencies installed
    )
) else (
    echo [INFO] No management package.json found, skipping
)
echo.

REM Verify installation
echo [STEP 6] Verifying Installation
echo ========================================

cd /d "%DEPLOY_DIR%"

echo [INFO] Verifying PM2...
pm2 --version >nul 2>&1
if %errorlevel% equ 0 (
    for /f "tokens=*" %%i in ('pm2 --version') do echo [OK] PM2 version: %%i
) else (
    echo [ERROR] PM2 verification failed
)

echo [INFO] Verifying backend dependencies...
if exist "%APP_DIR%\backend\node_modules" (
    echo [OK] Backend node_modules exists
) else (
    echo [ERROR] Backend node_modules missing
)

echo [INFO] Verifying frontend dependencies...
if exist "%APP_DIR%\frontend\node_modules" (
    echo [OK] Frontend node_modules exists
) else (
    echo [ERROR] Frontend node_modules missing
)
echo.

REM Clean npm cache
echo [STEP 7] Cleanup
echo ========================================

echo [INFO] Cleaning npm cache...
npm cache clean --force >nul 2>&1
echo [OK] npm cache cleaned
echo.

echo ========================================
echo DEPENDENCIES INSTALLATION COMPLETED!
echo ========================================
echo.
echo Next steps:
echo 1. Run: deploy-en.bat (for full deployment)
echo 2. Or run: start-services-en.bat (to start services only)
echo.

pause
